<?php
declare (strict_types = 1);

namespace app\auth\controller;

use think\Request;
use think\facade\Env;
use think\facade\View;
use fortuna\Email;
use think\facade\Session;
class Admin extends AuthBase
{
    private $app_title;
    protected $middleware = [ 
    	'\app\middleware\CheckToken' 	=> ['only' 	=> ['info'] ],
    ];
    // 初始化
    protected function initialize()
    {
        parent::initialize();
        $this->model = new \app\auth\model\Admin;
        $this->validate = new \app\auth\validate\Admin;
        $this->app_title = Env::get('app.title','');
        View::assign('app_title',$this->app_title);
    }
    // 管理员登录
    public function login()
    {
        if($this->request->isGet()) {
            return View::fetch(); 
        } else {
            $data = $this->request->param();
            if(!$this->validate->scene('login')->check($data)){
                return json($this->validate->getError(),500);
            }
            // 图形验证码

            // 查找用户
            $admin_info = $this->model->where('username', $data['username'])->find();
            if(!$admin_info){
                return json('用户不存在！',400);  
            }
            // 验证密码
            if (!$this->app->auth->verify_password($data['password'], $admin_info->getData('password'), $admin_info->getData('salt'))){
                return json('密码错误！',500);
            }
            $token = $this->issuing_token($admin_info['id']);
            return json($token);
        }
    }
    // 管理员注册
    public function register(){
        if($this->request->isGet()) {
            return View::fetch();
        } else {
            $data = $this->request->param();
            if($this->validate->scene('register')->check($data)){
                // 去掉多余数据
                unset($data['repassword']);
                list($data['password'],$data['salt']) = $this->app->auth->encrypt_password($data['password']);
                // 保存数据
                $this->model->save($data);
                return json('注册成功！');
            }else{
                return json($this->validate->getError(),500);
            }
        }
    }
    // 忘记密码
    public function forget_pwd(){
        if($this->request->isGet()) {
            return View::fetch();
        } else {
            $username = $this->request->param('username',null);
            $email = $this->request->param('email', null);
            $admin_info = $this->model->where(['username'=>$username,'email'=>$email])->find();
            if(!$admin_info){
                return json('用户名称或者邮箱错误！',500);  
            }
            // send email
            $e = new Email();
            $sercet = $this->app->auth->genrate_timeout_sercet($username);
            $url = $this->request->param('url', null);
            $url = $url.'?username='.$username.'&sercet='.$sercet.'&email='.$email;
            // $url = $this->request->domain().'/auth/admin/change_pwd?username='.$username.'&sercet='.$sercet;
            $e->sendForgetPwd($email,$username,$url);
            return json('发送成功！请前往邮箱查看邮件！');
        }
    }
    /**
     * 修改密码
     *
     * @return void
     */
    public function change_pwd()
    {
        if($this->request->isGet()) {
            return View::fetch();
        } else {
            // 验证sercet
            $sercet = $this->request->param('sercet', null);
            $email = $this->request->param('email', null);
            $username = $this->request->param('username', null);
            if(!$this->app->auth->verify_timeout_sercet($sercet,$username)){
                return json('code error!', 500);
            }
            // 
            $admin_info = $this->model->where(['username'=>$username,'email'=>$email])->find();
            $new_input_password = $this->request->param('new_password',null);
            // if(!$this->app->auth->verify_password($old_input_password,$admin_info['password'],$admin_info['salt'])){
                // return json('旧密码错误！',500);
            // } else {
            list($admin_info['password'],$admin_info['salt']) = $this->app->auth->encrypt_password($new_input_password);
            $admin_info->save();
            return json('修改密码成功！');
        }
    }
    /**
     * 退出方法
     *
     * @return json
     */
    public function logout(){
        return json('成功退出！');

    }
    // 获取/更新个人信息
    public function info(){
        if($this->request->isGet()) {
            $info = $this->model->field('id,username,truename,email,role,mobile,avatar,status')->where('id',$this->request->uid)->find();
            return json($info);
        } else {
            // 更新个人信息
            $data = $this->request->param();
            if(!$this->validate->scene('update')->check($data)){
                return json($this->validate->getError(),500);
            }else {
                if(!empty($data['password'])){
                    // 重置密码
                    list($data['password'],$data['salt']) = $this->app->auth->encrypt_password($data['password']);
                }
                $this->model->update($data,['id'=>$data['id']]);
                return json('数据更新成功！');
            }
        }
    }
}
